// pages/mine/pages/my/bankcard-add/bankcard-add.js
// pages/my/change-phoneNumber/change-phoneNumber.js
const request = require('../../../../../utils/request')
const common = require('../../../../../utils/common')
Page({

    /**
     * 页面的初始数据
     */
    data: {
        account_name: '',
        id_card: '',
        card_number: '',
        phone: '',
        bankid: '',
        code: '',
        bankObj:null,

        // 倒计时
        timeText: '获取验证码',
        timeWait: 60,
        timeBack: 'rgba(238,126,48,1)',
        successCode: true,

        index: '',
        bankList: [],
         
        userBankid:'' //修改id


    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad(options) {
        this.getBankList()

        console.log(options)
        if(options.id) {
         this.setData({
            userBankid:options.id
         })
         this.getDetail()
        }

    },
    getDetail(){
        request._post('/api?method=user.getbankcardinfo', {
          id:this.data.userBankid
        }, 'noload').then(res => {
            console.log(res)
            let { account_name, id_card, card_number, phone } = res.data
            this.setData({
                account_name,
                id_card,
                card_number,
                phone,
                bankid:res.data.bankid,
                bankObj:{
                    name:res.data.bank_name,
                }
            })
        })
      },
    inputWrite(e) {
        console.log(e, 2323)
        this.setData({
            [e.currentTarget.dataset.name]: e.detail.value
        })
    },
    bindPickerChange: function (e) {
        console.log('picker发送选择改变，携带值为', e.detail.value)
        this.setData({
            bankid:this.data.bankList[e.detail.value].id,
            bankObj:this.data.bankList[e.detail.value]
        })
    },
    // 获取验证码
    getYzCode() {
        let _this = this
        console.log(this.data.phone)
        // 判断是否填写手机号
        if (!this.data.phone) {
            wx.showToast({
                icon: 'none',
                title: '请先填写手机号码',
                duration: 500
            })
        } else if (!common.isMobile(this.data.phone)) {
            wx.showToast({
                icon: 'none',
                title: '请填写正确的手机号码格式',
                duration: 500
            })
        } else {
            // 开启倒计时
            if (!_this.data.successCode) {
                return
            }
            this.getCodeApi()
        }
    },
    // 获取验证码接口
    getCodeApi() {
        // 判定手机号是否被注册
        let _this = this
        // if (!this.data.verify_code) {
        //     wx.showToast({
        //         title: '请填写校验码',
        //         icon: 'none'
        //     })
        //     return
        // }
        request._get('/api?method=user.sms', {
            mobile: this.data.phone,
            code: 'veri'
        }, 'noload').then(res => {
            wx.showToast({
                title: res.msg,
                icon: 'none'
            })
            _this.startTime()
        })
    },
    // 验证码倒计时
    startTime() {
        let _this = this
        var inter = setInterval(function () {
            _this.setData({
                timeWait: _this.data.timeWait - 1,
                timeText: _this.data.timeWait + 's',
                timeBack: 'rgba(245,245,245,1)',
            })
            if (_this.data.timeWait < 0) {
                clearInterval(inter)
                _this.setData({
                    timeWait: 60,
                    timeText: '获取验证码',
                    timeBack: 'rgba(238,126,48,1)',
                    successCode: true
                })
            }
        }.bind(_this), 1000)
        if (_this.data.successCode) {
            _this.setData({
                successCode: false
            })
        }
    },
    idCard(value) {
        return /^[1-9]\d{5}[1-9]\d{3}((0\d)|(1[0-2]))(([0|1|2]\d)|3[0-1])\d{3}([0-9]|X)$/.test(
            value
        )
    },
    // 提交
    submit() {
        let { account_name, id_card, bankid, card_number, phone, code } = this.data
        if (!account_name) {
            wx.showToast({
                title: '请输入持卡人姓名',
                icon:'none'
            })
            return
        }
        if (!id_card) {
            wx.showToast({
                title: '请输入身份证号码',
                icon:'none'
            })
            return
        }
        if (!this.idCard(id_card)) {
            wx.showToast({
                title: '请输入正确的身份证号码',
                icon:'none'
            })
            return
        }
        if (!bankid) {
            wx.showToast({
                title: '请选择银行',
                icon:'none'
            })
            return
        }
        if (!card_number) {
            wx.showToast({
                title: '请输入银行卡号',
                icon:'none'
            })
            return
        }
        if (!phone) {
            wx.showToast({
                title: '请输入手机号码',
                icon:'none'
            })
            return
        }
        // if (!code) {
        //     wx.showToast({
        //         title: '请输入手机验证码',
        //         icon:'none'
        //     })
        //     return
        // }
        if(this.data.userBankid) {
            request._post('/api?method=user.editbankcard', {
                account_name,
                id_card,
                bankid,
                card_number,
                phone,
                // code,
                id:this.data.userBankid
            }, 'noload').then(res => {
                wx.showToast({
                    title: res.msg,
                    icon: 'none'
                })
                setTimeout(function() {
                    wx.navigateBack()
                },1000)
    
            })

        } else {
            request._post('/api?method=user.addbankcard', {
                account_name,
                id_card,
                bankid,
                card_number,
                phone,
                code,
            }, 'noload').then(res => {
                wx.showToast({
                    title: res.msg,
                    icon: 'none'
                })
                setTimeout(function() {
                    wx.navigateBack()
                },1000)
    
            })
        }
       


    },
    // 获取银行列表
    getBankList() {
        request._post('/api?method=user.banklist', {

        }, 'noload').then(res => {
            this.setData({
                bankList:res.data
            })
        })
    },
    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady() {

    },


    /**
     * 生命周期函数--监听页面显示
     */
    onShow() {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide() {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload() {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh() {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom() {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage() {

    }
})